﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeMakeSP.Platform.SharePoint.ServiceModel.Services.ServiceContract;
using WeMakeSP.Platform.SharePoint.ServiceModel.Services.ServiceImplementation;
using WeMakeSP.Platform.SharePoint.BusinessEntities;

namespace WeMakeSP.SiteManagement.ServiceModel.Client
{
    /// <summary>
    /// Represents a facade for available services
    /// in the site management context.
    /// </summary>
    public class SiteManagementServices
    {
        #region Fields

        /// <summary>
        /// The site url.
        /// </summary>
        private string siteUrl;

        /// <summary>
        /// Represents a webs service field.
        /// </summary>
        private IWebsService websServiceField; 

        #endregion

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SiteManagementServices"/> class.
        /// </summary>
        public SiteManagementServices()
        {
            this.siteUrl = "http://localhost";
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SiteManagementServices"/> class.
        /// </summary>
        /// <param name="siteUrl">The site URL.</param>
        public SiteManagementServices(string siteUrl)
        {
            // Guard.

            if (string.IsNullOrEmpty(siteUrl))
            {
                throw new ArgumentNullException("siteUrl");
            }

            this.siteUrl = siteUrl;
        }

        #endregion

        #region Private Properties

        /// <summary>
        /// Gets the webs.
        /// </summary>
        /// <value>The webs.</value>
        private IWebsService Webs
        {
            get
            {
                if (this.websServiceField == null)
                {
                    this.websServiceField =
                        new WebsService(this.siteUrl);
                }
                
                return
                    this.websServiceField;
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Gets the web configuration.
        /// </summary>
        /// <param name="webName">Name of the web.</param>
        /// <returns></returns>
        public SPWebConfiguration GetWebConfiguration(string webName)
        {
            return 
                this.Webs.GetWebConfigurationByName(webName);
        }

        /// <summary>
        /// Updates the master page.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="masterPage">The master page.</param>
        public void UpdateMasterPage(Guid webId, SPMasterPage masterPage)
        {
            this.Webs.UpdateMasterPageByWebId(webId, masterPage);
        }

        /// <summary>
        /// Updates the web logo.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="webLogo">The web logo.</param>
        public void UpdateWebLogo(Guid webId, SPWebLogo webLogo)
        {
            this.Webs.UpdateSiteLogoByWebId(webId, webLogo);
        }

        /// <summary>
        /// Updates the web theme.
        /// </summary>
        /// <param name="webId">The web id.</param>
        /// <param name="theme">The theme.</param>
        public void UpdateWebTheme(Guid webId,string theme)
        {
            this.Webs.UpdateSiteThemeByWebId(webId, theme);
        }

        #endregion
    }
}
